Created variable inside for loop can't be accessed when using a class